package org.elanfox.intellicommunity.intellicommunityadmin.scheduled;

import lombok.extern.slf4j.Slf4j;
import org.elanfox.intellicommunity.biz.entity.equipment.BaseStationPO;
import org.elanfox.intellicommunity.biz.entity.equipment.LocatorPO;
import org.elanfox.intellicommunity.biz.service.core.equipment.BaseStationService;
import org.elanfox.intellicommunity.biz.service.core.equipment.LocatorService;
import org.elanfox.intellicommunity.biz.service.core.statistics.AlarmStatisticsService;
import org.elanfox.intellicommunity.common.constant.RedisConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/7/23
 */
@Service
@Slf4j
public class MScheduleService {
    @Autowired
    private AlarmStatisticsService alarmStatisticsService;
    @Autowired
    private BaseStationService baseStationService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private LocatorService locatorService;

    /**
     * 一些统计
     * 每天02点执行
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void orderStatistics() {
        log.info("报警统计开始...");
        LocalDate date = LocalDate.now().minusDays(1);
        try {
            alarmStatisticsService.statistics(date);
        } catch (Exception ex) {
            log.error("报警统计失败---" + ex.getMessage(), ex);
        }
    }


    /**
     * 心跳监测
     * 5分钟一次
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void heartbeat() {
        log.info("心跳监测...");
        //得到所有的设备
        List<BaseStationPO> stationPOS = baseStationService.findAll();

        for (BaseStationPO stationPO : stationPOS) {
            Long time = (Long) redisTemplate.opsForHash().get(RedisConstant.KEY_HEARTBEAT, String.valueOf(stationPO.getDeviceAddr()));
            if (time == null || time < System.currentTimeMillis() - 604800000L) {
                log.info(stationPO.getDeviceAddr() + "离线了...");
                //离线了
                stationPO.setOnlineStatus(false);
            } else {
                stationPO.setOnlineStatus(true);
            }
            baseStationService.save(stationPO);
        }
    }

    /**
     * 定位器的心跳监测
     * 每天晚上1点
     */
    @Scheduled(cron = "0 0 3 * * ?")
    public void locatorHeartbeat() {
        log.info("定位器的心跳监测");
        List<LocatorPO> locatorPOS = locatorService.findAll();
        log.info("共获取到定位器" + locatorPOS.size());
        for (LocatorPO locatorPO : locatorPOS) {
            Long time = (Long) redisTemplate.opsForHash().get(RedisConstant.KEY_LOCATOR_HEARTBEAT, String.valueOf(locatorPO.getNo()));
            //两天没收到，证明离线了
            if (time == null || time < System.currentTimeMillis() - 604800000L) {
                log.info(locatorPO.getNo() + "离线了...");
                //离线了
                locatorPO.setOnlineStatus(false);
            } else {
                locatorPO.setOnlineStatus(true);
            }
            locatorService.save(locatorPO);
        }
    }

    public static void main(String[] args) {

        System.out.println(new Date(1611015685028L - 604800L));
    }
}
